<?php

declare(strict_types=1);

namespace App\Command;

use Hyperf\Command\Command as HyperfCommand;
use Hyperf\Command\Annotation\Command;
use Psr\Container\ContainerInterface;
use App\Service\CommodityService\SupplierCommodityPullLogService;
use Symfony\Component\Console\Input\InputArgument;

/**
 * 供应商商品API接口数据拉取任务中心
 * @Command
 */
class SupplierCommodityPullTask extends HyperfCommand
{
    /**
     * @var ContainerInterface
     */
    protected $container;
    
    /**
     * @var string
     */
    protected $SupplierID;
    
    const TIME_WAIT = 10;
    
    public function __construct(ContainerInterface $container)
    {
        $this->container = $container;

        parent::__construct('SupplierCommodityPullTask');
    }

    public function configure()
    {
        parent::configure();
        $this->setDescription('供应商商品API接口数据拉取');
    }

    /**
     * 开始执行命令
     * 单个供应商 php bin/hyperf.php SupplierCommodityPullTask 325653940228065154 
     * 所有供应商依次处理 php bin/hyperf.php SupplierCommodityPullTask
     */
    public function handle()
    {
        $args = $this->input->getArguments();
        $SupplierID = $args['SupplierID'] ?? 0 ;
        //单个供应商
        if( $SupplierID > 0 ) {
            SupplierCommodityPullLogService::instance()->runTask($SupplierID);
        } else {
            while ( true )
            {
                SupplierCommodityPullLogService::instance()->runTask($SupplierID);
                sleep(self::TIME_WAIT);
            }
        }
        $this->line('finished run task', 'info');
    }
    
    protected function getArguments()
    {
        return [
            ['SupplierID', InputArgument::OPTIONAL, '单个供应商ID']
        ];
    }
}
